﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AudioGrabber.Interfaces;
using VkcomAPI.PublicTypes;
using System.Net;
using System.IO.IsolatedStorage;

namespace AudioGrabber
{
    /// <summary>
    /// Interaction logic for LoginDialog.xaml
    /// </summary>
    public partial class LoginDialog : Window
    {
        private Func<UserCredentials, bool> authMethod;
        private bool isAuthorizationSucceded = false;
        private ICredentialsHelper credentialHelper;

        public LoginDialog(Func<UserCredentials, bool> authMethod)
        {
            InitializeComponent();
            this.authMethod = authMethod;
            IsolatedStorageFile isoFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
            credentialHelper = new FileCredentialSaver(listBox1, isoFile);
            credentialHelper.LoadSavedCredentials();

            if (listBox1.Items.Count > 0)
            {
                listBox1.SelectedIndex = 0;
                tabControl.SelectedIndex = 1;
            }
        }

        public bool Authorize()
        {
            try
            {
                this.ShowDialog();
                isAuthorizationSucceded = true;
            }
            catch (WebException ex)
            {
                isAuthorizationSucceded = false;
                this.Close();
                MessageBox.Show(ex.Message);
            }
            return isAuthorizationSucceded;
        }

        private void AuthorizeInternal(UserCredentials userCredentials)
        {
            isAuthorizationSucceded = authMethod(userCredentials);
            if (!isAuthorizationSucceded)
                MessageBox.Show("Authorization failed - try again");

            if (saveToListCB.HasContent && saveToListCB.IsChecked.Value)
                credentialHelper.SaveToStorage(userCredentials);

            Close();
        }

        #region Login tab item

        private void loginEmailBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            loginAuthorizeButton.IsEnabled = !string.IsNullOrEmpty(loginEmailBox.Text);
        }

        private void loginPasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            loginAuthorizeButton.IsEnabled = !string.IsNullOrEmpty(loginPasswordBox.Password);
        }

        private void loginAuthorizeButton_Click(object sender, RoutedEventArgs e)
        {
            UserCredentials userCredentials = new UserCredentials(loginEmailBox.Text, loginPasswordBox.Password);
            AuthorizeInternal(userCredentials);
        }

        #endregion

        #region Choose tab item

        private void chooseLoginWithSelected_Click(object sender, RoutedEventArgs e)
        {
            UserCredentials selectedCredentials = listBox1.SelectedItem as UserCredentials;
            if (selectedCredentials == null)
                return;

            AuthorizeInternal(selectedCredentials);
        }

        private void chooseRemoveFromList_Click(object sender, RoutedEventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
                return;

            UserCredentials selectedCredentials = (UserCredentials)listBox1.SelectedItem;
            credentialHelper.DeleteFromStorage(selectedCredentials);
        }

        private void chooseEmailBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            chooseAddButton.IsEnabled = (sender as TextBox).Text.Length > 0;
        }

        private void choosePasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            chooseAddButton.IsEnabled = (sender as PasswordBox).Password.Length > 0;
        }

        private void chooseAddButton_Click(object sender, RoutedEventArgs e)
        {
            credentialHelper.SaveToStorage(new UserCredentials(chooseEmailBox.Text, choosePasswordBox.Password));
            credentialHelper.LoadSavedCredentials();
        }

        #endregion

    }
}
